package com.wolfheader.wolfgame.service.admin.impl;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wolfheader.wolfgame.bean.admin.PermissionBean;
import com.wolfheader.wolfgame.dao.admin.PermissionDao;
import com.wolfheader.wolfgame.entity.admin.PermissionEntity;
import com.wolfheader.wolfgame.service.admin.PermissionService;
import com.wolfheader.wolfgame.service.impl.BaseServiceImpl;
import com.wolfheader.wolfgame.shiro.AdminShiroRealm;

@Service("permissionServiceImpl")
public class PermissionServiceImpl extends BaseServiceImpl<PermissionEntity> implements PermissionService {

	@Autowired
	private PermissionDao permissionDao;

	@Autowired
	private AdminShiroRealm adminShiroRealm;

	@Override
	public void addPermission(PermissionBean bean) {
		PermissionEntity entity = new PermissionEntity();
		entity.setContent(bean.getCont());
		entity.setIntro(bean.getIntro());
		entity.setPath(bean.getPath());
		entity.setPermissionname(bean.getPermissionname());
		// TODO Auto-generated method stub
		adminShiroRealm.clearAllCachedAuthorizationInfo();
		permissionDao.save(entity);
	}

	@Override
	public boolean modif(Long id, PermissionBean bean) {
		PermissionEntity entity = permissionDao.findOne(id);
		if (!entity.isEmpty()) {
			entity.setContent(bean.getCont());
			entity.setIntro(bean.getIntro());
			entity.setPermissionname(bean.getPermissionname());
			entity.setPath(bean.getPath());
			permissionDao.save(entity);
			adminShiroRealm.clearAllCachedAuthorizationInfo();
			return true;
		}
		return false;
	}

	@Transactional
	public void delete(Long id) {
		permissionDao.delete(id);
		adminShiroRealm.clearAllCachedAuthorizationInfo();
	}

}
